{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "76da2538",
   "metadata": {},
   "source": [
    "Examples require an initialized GsSession and relevant entitlements. External clients need to substitute thier own client id and client secret below. Please refer to [Authentication](https://developer.gs.com/p/docs/institutional/platform/authentication/) for details."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a2dd9568",
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.session import GsSession, Environment\n",
    "from datetime import date\n",
    "from gs_quant.data import PTPDataset\n",
    "import pydash\n",
    "import pandas as pd\n",
    "\n",
    "GsSession.use(Environment.PROD, client_id=None, client_secret=None, scopes=('read_product_data',))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "402e5482",
   "metadata": {},
   "source": [
    "# PTP Datasets\n",
    "\n",
    "PlotTool Pro provides a way to easily upload and visualize your data stored with Marquee Data Services, and you can use `gs_quant`'s `PTPDataset` class to upload, save, and view your `pandas` data structures. \n",
    "\n",
    "For now, PTP datasets are only available for internal Marquee users.\n",
    "\n",
    "Data uploaded to a PTP dataset must be\n",
    "1. Numeric\n",
    "1. Indexed by a `pd.DatetimeIndex`\n",
    "1. EOD (as opposed to real-time)\n",
    "\n",
    "For further information, see the `PTPDataset` class. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e325a576",
   "metadata": {},
   "source": [
    "## Workflow"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41a9f73e",
   "metadata": {},
   "source": [
    "#### Create a PTP Dataset\n",
    "\n",
    "When creating a new PTP dataset, you first initialize an object, passing in a `pandas` Series or DataFrame, and an optional name. If you don't provide a name, the dataset's name will default to \"GSQ Default.\" \n",
    "\n",
    "If you pass a DataFrame, the field names in your dataset will be derived from the DataFrame's column names; if you provide a Series, the field name will be taken from the series name (if it has one), or otherwise \"values\" by de\n",
    "\n",
    "**Note**: your data is not *saved* until you call `.sync()` on your PTP dataset object. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b52dae72",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame({'col': range(50), 'fieldb': range(100, 150)}, index=pd.date_range(start=date(2021, 1, 1), periods=50, freq='D'))\n",
    "dataset = PTPDataset(df, 'Test Dataset')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb2aad49",
   "metadata": {},
   "source": [
    "#### Sync\n",
    "\n",
    "Sync your dataset to save your data. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4e0cf7f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset.sync()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b25a2be3",
   "metadata": {},
   "source": [
    "#### Plot\n",
    "\n",
    "Generate a transient plot expression, which will bring you to a plot displaying your data. \n",
    "\n",
    "Make sure to hit \"Copy to My Plots\" to save the plot (although you can always re-generate another transient plot expression). \n",
    "\n",
    "If you're running `gs_quant` on a device with a default browser, `.plot()` will automatically open the plot for you. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "afd058d6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'https://marquee.web.gs.com/s/plottool/transient?expr=Dataset(\"PTP_TEST_DATASET_LOAK1\").col()%0ADataset(\"PTP_TEST_DATASET_LOAK1\").fieldb()'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset.plot(open_in_browser=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb870105",
   "metadata": {},
   "source": [
    "#### Other dataset functions\n",
    "\n",
    "Since `PTPDataset` inherits from the `Dataset` class, it has all the functionality of a normal `Dataset` object in addition to the methods above. \n",
    "\n",
    "For example, to delete a dataset: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "853c639d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Successfully deleted dataset.'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset.delete()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
